<?php

class homeComponents extends sfComponents {

    public function executeAssigned_programs() {
        $teacher_id = $this->getUser()->getGuardUser()->getUsername();
        $q = Doctrine_query::create()
                ->select('p.start_date, p.name, p.program_type as kind')
                ->from('Program p')
                ->leftJoin('p.LinkTeacherProgram link')
                ->where('link.assign_status = 2')
                ->andWhere('link.teacher_id = ?', $teacher_id)
                ->andWhere('p.global_program_type = ?', 'night');
        $results_1 = $q->fetchArray();


        // long programs

        $q = Doctrine_Query::create()
                ->from('LongTermProgram p')
                ->select('p.name, p.start_date, "long" as kind')
                ->leftJoin('p.LongTermProgramApplication app')
                ->where('app.is_approved = 1')
                ->andWhere('app.teacher_id = ?', $teacher_id);

        $results_2 = $q->fetchArray();
        $this->results = array_merge($results_1, $results_2);
    }

    public function executeOpened_night_programs() {

        $ay = AcademicYearTable::getInstance()->getDefaults();
        $ayid = $ay['id'];
        $teacher_id = $this->getUser()->getGuardUser()->getUsername();

        $hc = new hijreeConvertor();
        $hijree_today = $hc->GregorianToIslamic2(time());


        // --- exclude assigned and pending programs

        $q = Doctrine_query::create()
                ->select('GROUP_CONCAT(p.id)')
                ->from('Program p')
                ->leftJoin('p.LinkTeacherProgram link')
                ->where('link.teacher_id = ?', $teacher_id);
        $exclude_programs = $q->fetchOne(array(), 6);

        // ---
        // --- get course_id

        $q = Doctrine_query::create()
                ->select('tc.course_id')
                ->from('TeacherCourse tc')
                ->where('tc.teacher_id = ?', $teacher_id)
                ->andWhere('tc.academic_year_id = ?', $ayid);

        $course_id = $q->fetchOne(array(), 6);

        // --- 
        // --- 

        $q = Doctrine_query::create()
                ->from('Program p1')
                ->where('p1.target_number > (SELECT COUNT(link.teacher_id) FROM LinkTeacherProgram link WHERE link.program_id = p1.id)')
                ->andWhere('p1.course_id = ?', $course_id)
                ->andWhere('p1.start_date > ?', $hijree_today)
                ->andWhere('p1.global_program_type = ?', 'night')
        ;

        if ($exclude_programs)
            $q->andWhere("p1.id NOT IN ($exclude_programs)");


        $this->results = $q->fetchArray();


        // --- 
    }

    public function executeLong_term_programs() {

        $ay = AcademicYearTable::getInstance()->getDefaults();
        $ayid = $ay['id'];
        $teacher_id = $this->getUser()->getGuardUser()->getUsername();

        // --- get course_id

        $q = Doctrine_query::create()
                ->select('tc.course_id')
                ->from('TeacherCourse tc')
                ->where('tc.teacher_id = ?', $teacher_id)
                ->andWhere('tc.academic_year_id = ?', $ayid);

        $course_id = $q->fetchOne(array(), 6);

        // --- 

        $hobj = new hijreeConvertor();
        $hdate = $hobj->GregorianToIslamic2(time());
        $q = Doctrine_Query::create()->from('LongTermProgram')
                ->where('is_active = 1')
                ->addWhere('start_date <= ?', $hdate)
                ->addWhere('end_date >= ?', $hdate)
                ->addWhere('course_id = ?', $course_id);
        $this->results = $q->fetchArray();

        // get applications for current teacher
        $user_id = intval($this->getUser()->getGuardUser()->getUsername());

        $q = Doctrine_Query::create()->from('LongTermProgramApplication')->where('teacher_id = ?', $user_id);

        $ids = array();
        foreach ($q->fetchArray() as $one)
            $ids[] = $one['long_program_id'];

        $this->ids = $ids;
    }

}